module App::Controller
    class Settings < Qt::Object
        def initialize()
            super()

            @view = App::View::Settings.new
            # Yep, it's an ugly hack against QtRuby segfaults.
            # If the 'dispose' wont be called on the close of the view,
            #  we're on the green mile! (you can try it if close w/ Alt+F4)
            connect(@view.buttonbox, SIGNAL(:accepted)) { accept }
            connect(@view.buttonbox.button(Qt::DialogButtonBox::Apply), SIGNAL(:clicked)) { apply }
            #connect(@view.buttonbox, SIGNAL(:rejected), @view, SLOT(:close))
            connect(@view.buttonbox, SIGNAL(:rejected)) { dispose }
            #connect(@view, SIGNAL(:destroyed)) { dispose }

            connect(@view.pushbutton_add, SIGNAL(:clicked)) { new_ignored_client }
            connect(@view.pushbutton_del, SIGNAL(:clicked)) { del_ignored_client }
        end

        private
        def accept
            apply
            dispose
        end

        def apply
            $widget.model.store_ignored_clients(@view.ignored_clients.model.all.map{|item| item.text})
            # XXX emit?
            $widget.treecontroller.filter_clients
            $widget.model.save_setting "hide_older_than", @view.hide_older_than.value
            $widget.model.dropwin_style = @view.textedit.to_plain_text
            $widget.model.reload_settings
        end

        def new_ignored_client
            new_item = $widget.model.ignored_clients.add "..."
            @view.ignored_clients.edit(new_item.index)
        end

        def del_ignored_client
            $widget.model.ignored_clients.del @view.ignored_clients.selectedIndexes
        end

        def dispose
            #puts "dispose!"
            @view.close
            @view.dispose
            super()
        end
    end
end
